package com.c.ctgproject.ui.collect

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryInputConfirmAdapter
import com.c.ctgproject.adapter.guang.BatteryInputOtherAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPowerConfirmInBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.BatteryInpuOtherEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe

/**
 *作者:LiBW
 *创建日期: 2021/10/28
 *描述: 散件入库 页面(动力电池)
 */
class PowerConfirmInActivity : BaseActivityV2(true) ,BatteryInputConfirmAdapter.BatteryInputConfirmCallback{
    private val mBinding by lazy { ActivityPowerConfirmInBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search = ""
    private val mList = ArrayList<BatteryInpuOtherEntity.DataBean.ListBean>()
    private val mAdapter by lazy { BatteryInputConfirmAdapter(this, mList, listener = this) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)
    }

    var searchMine = false
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "入库" -> {
                    searchMine = false
                    page = 1
                    getData()
                }
                "我的" -> {
                    searchMine = true
                    page = 1
                    getData()
                }
            }
        }


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private fun getData() {
        if (searchMine) {
            //查询类型，不传等价于传1, 【1-我创建的 2-我过磅的 3-我入库的】
            ApiMethods.apiSubscribe(ApiMethods.retrofit.mineInWaitWidget(3, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的入库:$it")
                val data = getGson().fromJson(it, BatteryInpuOtherEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) {
                        mList.clear()
                        //加上这两行避免在加载更多同时切换状态栏时发生显示错位问题
                        mBinding.mRefresh.finishLoadMore()
                        mBinding.mRefresh.closeHeaderOrFooter()
                    }
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerview.handlerPosition(page)

                }
            })
        } else {
            //查询类型 2-待过磅 3-待入库
            ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryWaitIn(3, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("电池待入库列表 :$it")
                val data = getGson().fromJson(it, BatteryInpuOtherEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerview.handlerPosition(page)
                }
            })
        }
    }

    override fun itemDelete(position: Int, data: BatteryInpuOtherEntity.DataBean.ListBean) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.deletePowerBattery(data.pbId), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("删除结果:$it")
            val result= getGson().fromString<ResultEntity<String>>(it)
            if(result.status==1){
                mList.removeAt(position)
                mAdapter.notifyDataSetChanged()
            }
        })
    }


}